// console.log(Buffer.alloc(10))

// console.log(Buffer.alloc(20))

// console.log(Buffer.alloc(5, 1))

// console.log(Buffer.allocUnsafe(5, 1))

console.log(Buffer.from([1,2,3]))

console.log(Buffer.from('test!'))

console.log(Buffer.from('test!', 'base64'))

/*
* Buffer.byteLength 字符占的字节数
* Buffer.isBuffer
* Buffer.concat
* */

console.log(Buffer.byteLength('test'))

console.log(Buffer.byteLength('测试'))

console.log(Buffer.isBuffer('test'))
console.log(Buffer.isBuffer(Buffer.from([1,2,3])))

// concat
const buff1 = Buffer.from('This ')
const buff2 = Buffer.from('is ')
const buff3 = Buffer.from('Tom.')
console.log(Buffer.concat([buff1, buff2, buff3]))
console.log(Buffer.concat([buff1, buff2, buff3]).toString())


/*
* buf.length
* buf.toString()
* buf.fill()
* buf.equals()
* buf.indexOf()
* buf.copy()
* */
const buf4 = Buffer.allocUnsafe(10)
console.log(buf4.fill(5,3,7))
const buf5 = Buffer.from([12,231,12])
console.log(buf5.length)

const buf1 = Buffer.from('This ')
const buf2 = Buffer.from('is ')
const buf3 = Buffer.from('Tom.')
console.log(Buffer.concat([buf1, buf2, buf3]))
console.log(Buffer.concat([buf1, buf2, buf3]).toString())

// equals
const buf6 = Buffer.from('test')
const buf7 = Buffer.from('test')
const buf8 = Buffer.from('test1')

console.log(buf6.equals(buf7))
console.log(buf6.equals(buf8))

// indexOf
console.log(buf8.indexOf(buf6))

// copy
const buf9 = Buffer.from('test')
console.log(buf9.copy(buf8, buf9))

//
const stringDecoder = require('string_decoder').StringDecoder
const buf = Buffer.from('中文字符串！')
const decoder = new stringDecoder('utf8')

for(let i = 0;i < buf.length;i=i+5){
    const b = Buffer.allocUnsafe(5)
    buf.copy(b,0,i)

    console.log(b.toString())
}

//
for(let i = 0;i < buf.length;i=i+5){
    const b = Buffer.allocUnsafe(5)
    buf.copy(b,0,i)

    console.log(decoder.write(b))
}
